<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="id" label="ID" width="80"/>
            <el-table-column prop="cover" label="封面" width="120">
              <template slot-scope="scope">
                <el-image
                    :preview-src-list="[scope.row.cover]"
                    style="width: 80px; height: 80px"
                    :src="scope.row.cover" fit="cover">
                  <template #error>
                    <img src="@/assets/file.png" alt="" style="width: 80px; height: 80px">
                  </template>
                </el-image>
              </template>
            </el-table-column>
            <el-table-column prop="title" label="标题" min-width="200"/>
            <el-table-column prop="author" label="作者" width="100"/>
            <el-table-column prop="category_name" label="分类" width="120"/>
            <el-table-column prop="tags" label="标签" width="150">
              <template slot-scope="scope">
                <el-tag 
                  v-for="tag in parseTags(scope.row.tags)" 
                  :key="tag"
                  size="mini"
                  style="margin-right: 5px; margin-bottom: 5px;"
                >
                  {{ tag }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="view_count" label="浏览" width="80"/>
            <el-table-column prop="like_count" label="点赞" width="80"/>
            <el-table-column prop="sort" label="排序" width="80"/>
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 1" type="success">发布</el-tag>
                <el-tag v-else type="info">草稿</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="特殊标记" width="120">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.is_top === 1" type="warning" size="mini">置顶</el-tag>
                <el-tag v-if="scope.row.is_recommend === 1" type="danger" size="mini">推荐</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="publish_time" label="发布时间" width="160">
              <template slot-scope="scope">
                {{ formatDateTime(scope.row.publish_time) }}
              </template>
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="150">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";

export default {
  name: "ArticleIndex",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.article,
      searchList: [
        {
          label: '标题',
          prop: 'title',
          name: 'input',
          placeholder: '请输入文章标题'
        },
        {
          label: '作者',
          prop: 'author',
          name: 'input',
          placeholder: '请输入作者'
        },
        {
          label: '分类',
          prop: 'category_id',
          name: 'select',
          placeholder: '请选择分类',
          options: []
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          placeholder: '请选择状态',
          options: [
            { value: 1, label: '发布' },
            { value: 0, label: '草稿' }
          ]
        }
      ],
      searchParams: {
        title: '',
        author: '',
        category_id: '',
        status: ''
      },
      defaultParams: {
      },
      tableData: []
    }
  },
  mounted() {
    this.loadCategories();
  },
  methods: {
    loadCategories() {
      // 加载文章分类
      http.articleCategory.list().then(res => {
        this.searchList[2].options = res.data.map(item => ({
          value: item.id,
          label: item.name
        }));
      });
    },
    parseTags(tags) {
      if (!tags) return [];
      try {
        return JSON.parse(tags);
      } catch (e) {
        return [];
      }
    },
    formatDateTime(dateTime) {
      if (!dateTime) return '';
      return new Date(dateTime).toLocaleString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit'
      });
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style> 